package com.ruoyi.sales.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.List;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.core.domain.entity.ZcProcess;
import com.ruoyi.common.core.domain.entity.ZcStock;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.sales.domain.ZcSpecifications;
import com.ruoyi.sales.mapper.ZcStockMapper;
import com.ruoyi.system.service.ISysDictDataService;
import org.apache.poi.hssf.record.DVALRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.sales.mapper.ZcProductMapper;
import com.ruoyi.sales.domain.ZcProduct;
import com.ruoyi.sales.service.IZcProductService;

/**
 * 产品Service业务层处理
 *
 * @author woo
 * @date 2024-04-02
 */
@Service
public class ZcProductServiceImpl extends ServiceImpl<ZcProductMapper, ZcProduct> implements IZcProductService {
    @Autowired
    private ZcProductMapper zcProductMapper;

    @Autowired
    private ZcStockMapper zcStockMapper;

    @Autowired
    private ZcProcessServiceImpl processService;
    @Autowired
    private ZcStockServiceImpl stockService;
    @Autowired
    private ISysDictDataService dictDataService;
    @Autowired
    private ZcSpecificationsServiceImpl specificationsService;

    /**
     * 查询产品
     *
     * @param productId 产品主键
     * @return 产品
     */
    @Override
    public ZcProduct selectZcProductByProductId(Long productId) {
        return zcProductMapper.selectZcProductByProductId(productId);
    }

    /**
     * 查询产品列表
     *
     * @param zcProduct 产品
     * @return 产品
     */
    @Override
    public List<ZcProduct> selectZcProductList(ZcProduct zcProduct) {
        return zcProductMapper.selectZcProductList(zcProduct);
    }

    /**
     * 新增产品
     *
     * @param zcProduct 产品
     * @return 结果
     */
    @Override
    public int insertZcProduct(ZcProduct zcProduct) {
                zcProduct.setCreateTime(DateUtils.getNowDate());
                zcProduct.setDel((long) 0);
         // 产品名称
        changeProductName(zcProduct);

        // 产品编码
        changeProductCode(zcProduct);

        return zcProductMapper.insertZcProduct(zcProduct);
    }

    private void changeProductCode(ZcProduct zcProduct) {
        // 产品编码=工序id+品类id+规格number+颜色2-5位数
        // 工序id
        Long processId = zcProduct.getProcessId();
        // 品类id
        Long stockId = zcProduct.getStockId();
        // 规格number
        ZcSpecifications spec = specificationsService.selectZcSpecificationsBySpecificationsId(zcProduct.getSpecificationsId());
        Long number = spec.getNumber();
        // 颜色2-5位数
        String color = zcProduct.getColor().substring(1, 5);
        zcProduct.setCode(processId+"-"+stockId+"-"+number+"-"+color);
    }


    private void changeProductName(ZcProduct zcProduct) {
        // 产品名称=类别A类+性质防寒+品类高山羊毛+颜色黑色+品种羊毛
        ZcProcess zcProcess = processService.selectProcessById(zcProduct.getProcessId());
        // 类别A类
        String type = zcProcess.getParentName();
        // 性质防寒
        String nature = dictDataService.selectDictLabel("zc_product_nature", zcProduct.getNature());
        // 品类高山羊毛
        ZcStock zcStock = stockService.selectStockById(zcProduct.getStockId());
        String stockBreed = zcStock.getStockName();
        // 颜色黑色
        String color = zcProduct.getColor();
        // 品种羊毛
        String breed = zcStock.getParentName();
        zcProduct.setName(type+nature+stockBreed+color+breed);
    }

    /**
     * 修改产品
     *
     * @param zcProduct 产品
     * @return 结果
     */
    @Override
    public int updateZcProduct(ZcProduct zcProduct) {
                zcProduct.setUpdateTime(DateUtils.getNowDate());
                changeProductName(zcProduct);
                changeProductCode(zcProduct);
        return zcProductMapper.updateZcProduct(zcProduct);
    }



    /**
     * 批量删除产品
     *
     * @param productIds 需要删除的产品主键
     * @return 结果
     */
    @Override
    public int deleteZcProductByProductIds(Long[] productIds) {
        return zcProductMapper.deleteZcProductByProductIds(productIds);
    }

    /**
     * 删除产品信息
     *
     * @param productId 产品主键
     * @return 结果
     */
    @Override
    public int deleteZcProductByProductId(Long productId) {
        return zcProductMapper.deleteZcProductByProductId(productId);
    }

    @Override
    public List<ZcProduct> selectAllProducts() {
        QueryWrapper<ZcProduct> qw = new QueryWrapper<>();
        qw.eq("del",0).eq("status",0);
        List<ZcProduct> productList = zcProductMapper.selectList(qw);
        return productList;
    }

    @Override
    public int updateCodecStatus(ZcProduct zcProduct) {
        return zcProductMapper.updateCode(zcProduct);
    }
}